//
//  HomeViewController.m
//  inquiry
//
//  Created by 高宇 on 2020/5/21.
//  Copyright © 2020 高宇. All rights reserved.
//

#define SCREEN_WIDTH                 ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT                ([[UIScreen mainScreen] bounds].size.height)

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "QuestionViewController.h"
#import "GXToast.h"
#import "ULNetHelper.h"
#import "Interface.h"
#import "NSString+MD5.h"
#import "JsonObject.h"
#import "FinishViewController.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *arr;
@end

@implementation HomeViewController

//在页面出现的时候就将黑线隐藏起来
 -(void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏分割线
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    //TabBar背景色
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
    
    //创建UI
    [self createUI];
    [self getData];
}

- (void)createUI{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, SCREEN_WIDTH-60, 33)];
    label.text = @"欢迎来到，综合素养测评系统";
    label.font = [UIFont systemFontOfSize:24];
    label.textColor = [UIColor blackColor];
    [self.view addSubview:label];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(33, 102, SCREEN_WIDTH-60, 22)];
    NSString *label2Str = [NSString stringWithFormat:@"亲爱的%@同学，请完成以下问卷～",[[NSUserDefaults standardUserDefaults] objectForKey:@"studentName"]];
    label2.text = label2Str;
    label2.font = [UIFont systemFontOfSize:16];
    label2.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.view addSubview:label2];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(23, 150, SCREEN_WIDTH-46, SCREEN_HEIGHT-150-49) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 102;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    NSString *cellID = NSStringFromClass([HomeTableViewCell class]);
    [self.tableView registerNib:[UINib nibWithNibName:cellID bundle:nil] forCellReuseIdentifier:@"cell"];
    
}

- (void)getData{
    [ULNetHelper GET:queryNamePath parameters:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            self.arr = dic[@"data"];
            [self.tableView reloadData];
        }else{
            NSString *msg = responseObject[@"msg"];
            [GXToast showText:msg];
        }
    }];
    
}

//数据源方法的实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.arr.count>8) {
        return 8;
    }
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = self.arr[indexPath.row];
    NSString *text = dic[@"questionnaireName"];
    cell.label.text = text;
    switch (indexPath.row) {
        case 0:
            cell.downImageView.image = [UIImage imageNamed:@"第一条"];
            break;
        case 1:
            cell.downImageView.image = [UIImage imageNamed:@"第二条"];
            break;
        case 2:
            cell.downImageView.image = [UIImage imageNamed:@"第三条"];
            break;
        case 3:
            cell.downImageView.image = [UIImage imageNamed:@"person"];
            break;
        case 4:
            cell.downImageView.image = [UIImage imageNamed:@"第一条(1)"];
            break;
        case 5:
            cell.downImageView.image = [UIImage imageNamed:@"第二条(1)"];
            break;
        case 6:
            cell.downImageView.image = [UIImage imageNamed:@"第三条(1)"];
            break;
        case 7:
            cell.downImageView.image = [UIImage imageNamed:@"person(1)"];
            break;
            
        default:
            cell.downImageView.image = [UIImage imageNamed:@"第三条"];
            break;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"studentId"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"uuid"];
    dic[@"questionnaireId"] = self.arr[indexPath.row][@"questionnaireId"];
    dic[@"teacherId"] = @"";
    
    [ULNetHelper POST:querySolutionPath parameters:dic success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        long code = [dic[@"code"] longValue];
        if (code == 200) {
            NSString *dataStr = dic[@"data"];
            if ([dataStr isEqualToString:@"你已答题"]) {
                FinishViewController *vc = [[FinishViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                QuestionViewController *vc = [[QuestionViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                vc.questionnaireId = self.arr[indexPath.row][@"questionnaireId"];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }else{
            NSString *msg = responseObject[@"msg"];
            [GXToast showText:msg];
        }
    }];
    
}

- (void)sectionClick:(UIControl *)control{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
